﻿package 
{
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.external.ExternalInterface;

	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;

	public class Main extends Sprite
	{
		var keyArray:Array;
		var grid:Grid;
		var chartType:String;
		var chartName:String;
		var currentChart:Chart;
		
		public function Main():void
		{
			keyArray = new Array();

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			ChangeChartType('Line');

			stage.addEventListener(KeyboardEvent.KEY_DOWN, CheckKeysDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, CheckKeysUp);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, Clicked);
			ExternalInterface.addCallback("ChangeChartType", ChangeChartType);
			ExternalInterface.addCallback("SetChartName", SetChartName);
		}
		
		function ChangeChartType(newChartType:String) : void
		{
			if(newChartType == chartType)
			{
				return void;
			}
			
			chartType = newChartType;

			if(currentChart != null && contains(currentChart))
			{
				removeChild(currentChart);
			}
			
			RemoveGrid();
			
			switch(chartType) {
				case 'Pie':
					InitPie();
					break;
				case 'Bar':
					SetupGrid(14);
					InitBar();
					break;
				case 'ScatterPlot':
					SetupGrid(14);
					InitScatterPlot();
					break;
				case 'Radar':
					InitRadar();
					break;
				case 'Line':
				default:
					SetupGrid(14);
					InitLine();
					break;
			}
		}

		function InitRadar() : void
		{
			var radar:Radar = new Radar();
			radar.Init(stage.stageWidth, stage.stageHeight);
			if(currentChart != null && currentChart.dataPoints != null && currentChart.dataPoints.length > 0)
			{
				radar.dataPoints = currentChart.dataPoints;
				radar.ReValidateData();
			} else {
			}
			currentChart = radar;
			addChild(currentChart);
		}

		function InitScatterPlot() : void
		{
			var scatterplot:ScatterPlot = new ScatterPlot();
			scatterplot.Init(stage.stageWidth, stage.stageHeight);
			if(currentChart != null && currentChart.dataPoints != null && currentChart.dataPoints.length > 0)
			{
				scatterplot.dataPoints = currentChart.dataPoints;
				scatterplot.ReValidateData();
			} else {
			}
			currentChart = scatterplot;
			addChild(currentChart);
		}

		function InitBar() : void
		{
			var barchart:BarChart = new BarChart();
			if(currentChart != null && currentChart.dataPoints != null && currentChart.dataPoints.length > 0)
			{
				barchart.chartAttribute = currentChart.chartAttribute;
				barchart.dataPoints = currentChart.dataPoints;
				barchart.ReValidateData();
			} else {
			}
			barchart.Init(stage.stageWidth, stage.stageHeight);
			currentChart = barchart;
			addChild(currentChart);
		}

		function InitLine() : void
		{
			var linechart:LineChart = new LineChart();
			if(currentChart != null && currentChart.dataPoints != null && currentChart.dataPoints.length > 0)
			{
				linechart.chartAttribute = currentChart.chartAttribute;
				linechart.dataPoints = currentChart.dataPoints;
				linechart.ReValidateData();
			} else {
			}
			linechart.Init(stage.stageWidth, stage.stageHeight);
			currentChart = linechart;
			addChild(currentChart);
		}
		
		function InitPie() : void
		{
			var hw:Number = stage.stageWidth / 2;
			var hh:Number = stage.stageHeight / 2;
			var border:Number = Math.min(hw * 0.05, hh * 0.05);
			var piechart:PieChart = new PieChart();
			var radius:Number = Math.min(hw - (border * 2), hh - (border * 2));
			if(currentChart != null && currentChart.dataPoints != null && currentChart.dataPoints.length > 0)
			{
				piechart.chartAttribute = currentChart.chartAttribute;
				piechart.dataPoints = currentChart.dataPoints;
			}
			piechart.Init(hw - border, hh + border, radius);
			currentChart = piechart;
			addChild(currentChart);
		}
		
		function RemoveGrid()
		{
			if(getChildByName('grid') != null)
			{
				removeChild(getChildByName('grid'));
			}
		}
		
		function SetupGrid(size:Number = 0)
		{
			RemoveGrid();
			var grid:Grid = new Grid();
			grid.Init( Math.max(stage.stageWidth, stage.stageHeight), size);
			addChild(grid);
		}
		
		function SetChartName(newName:String) : void
		{
			chartName = newName;
		}
		
		function Clicked(e:MouseEvent) : void
		{
			ExternalInterface.call("ChartClicked", chartName);
		}
		
		function CheckKeysDown(event:KeyboardEvent):void
		{
			var key:String;

			if (event.charCode!=0) {
				key=String.fromCharCode(event.charCode);
			} else {
				key=event.keyCode.toString();
			}
			keyArray[key]=true;
		}

		function CheckKeysUp(event:KeyboardEvent):void
		{
			var key:String;

			if (event.charCode!=0) {
				key=String.fromCharCode(event.charCode);
			} else {
				key=event.keyCode.toString();
			}
			delete ( keyArray[key] );
		}
		
		public function isKeyDown(X:String) : Boolean
		{
			return keyArray[X];
		}
	}
}